﻿using System.Windows;
using Caliburn.Micro;
using JY.Wpf.Core.Enums;
using JY.Wpf.Core.Events;
using JY.Wpf.Core.Models;
using JY.Wpf.Core.Tools;
using JY.Wpf.Core.ViewModelBases;
using MahApps.Metro.Controls;
using Extensions = JY.Wpf.Core.Tools.Extensions;

namespace JY.Wpf.TestForMetro.ViewModels
{
    [NotifyPropertyChanged]
    public class ShellViewModel : MainViewModelBase
    {
        #region # 变量
        #endregion

        #region # 构造器
        /// <summary>
        /// 构造器
        /// </summary>
        public ShellViewModel()
        {
            this.Title = "立高智造";
            Wpf.Controls.Controls.WarningMessageBar.Initialize(this.DeactivateMessage);
            this.LeftMenuViewModel = new LeftMenuViewModel();
            //Sprite.Show(new FilpClockSprite());
        }
        #endregion

        #region # 属性

        #region # 当前用户——User CurUser

        /// <summary>
        /// 当前用户
        /// </summary>
        public User CurUser { get; set; }
        #endregion

        #region # 系统菜单——LeftMenuViewModel LeftMenuViewModel
        /// <summary>
        /// 系统菜单
        /// </summary>
        public LeftMenuViewModel LeftMenuViewModel { get; set; }
        #endregion

        #region # 是否全屏——bool IsFullScreen
        /// <summary>
        /// 是否全屏
        /// </summary>
        public bool IsFullScreen { get; set; }
        #endregion
        #endregion

        #region # Command方法

        #region # 全屏——void FullScreen()
        /// <summary>
        /// 全屏
        /// </summary>
        public void FullScreen()
        {
            this.IsFullScreen = true;
            this.SetFullScreen();
        }
        #endregion

        #region # 取消全屏——void CancleFullScreen()
        /// <summary>
        /// 取消全屏
        /// </summary>
        public void CancleFullScreen()
        {
            this.IsFullScreen = false;
            this.SetFullScreen();
        }
        #endregion
        #endregion

        #region # 方法
        #region # 子窗体关闭方法——void ChildWindowViewClosing()
        /// <summary>
        /// 子窗体关闭方法
        /// </summary>
        public override void ChildWindowViewClosing(RoutedEventArgs arg)
        {
            base.ChildWindowViewClosing(arg);
            //if (this.CurUser == null)
            //{
            //    this.EventAggregator.PublishOnUIThread(new WarningMessageEvent("请登录系统！"));
            //    //this.Handle(new ShowChildWindowEvent(new LoginViewModel("立高智造", "", true)));
            //}
        }
        #endregion

        #region # 完成警告消息回调方法——void DeactivateMessage()
        /// <summary>
        /// 完成警告消息回调方法
        /// </summary>
        private void DeactivateMessage(MessageType messageType)
        {
            //MessageBox.Info($"测试完成警告消息[{messageType}]回调方法。");
            this.EventAggregator.PublishOnUIThread(new MessageEvent($"测试完成警告消息[{messageType}]回调方法。",MessageType.Info));
        }
        #endregion

        #region # 设置全屏——void SetFullScreen()
        /// <summary>
        /// 设置全屏
        /// </summary>
        private void SetFullScreen()
        {
            var window = Application.Current.MainWindow as MetroWindow;
            if (window == null) return;
            if (this.IsFullScreen)
            {
                window.SetCurrentValue(MetroWindow.IgnoreTaskbarOnMaximizeProperty, true);
                window.SetCurrentValue(Window.WindowStateProperty, WindowState.Maximized);
                window.SetCurrentValue(MetroWindow.UseNoneWindowStyleProperty, true);
            }
            else
            {
                window.SetCurrentValue(Window.WindowStateProperty, WindowState.Normal);
                window.SetCurrentValue(MetroWindow.UseNoneWindowStyleProperty, false);
                window.SetCurrentValue(MetroWindow.ShowTitleBarProperty, true); // <-- this must be set to true
                window.SetCurrentValue(MetroWindow.IgnoreTaskbarOnMaximizeProperty, false);
            }
        } 
        #endregion
        #endregion

        #region # Events方法
        #region # 显示遮罩事件方法——void Handle(BusyIndicatorEvent busyIndicatorEvent)
        /// <summary>
        /// 显示遮罩事件方法
        /// </summary>
        /// <param name="busyIndicatorEvent">事件参数类</param>
        public override void Handle(BusyIndicatorEvent busyIndicatorEvent)
        {
            Extensions.BeginInvoke(Application.Current, () =>
            {
                this.IsBusy = busyIndicatorEvent.IsBusy;
                this.BusyContent = busyIndicatorEvent.BusyContent;
                Wpf.Controls.Controls.BusyIndicator.BusyOperation(this.IsBusy, this.BusyContent);
            });
        }
        #endregion

        #region # 显示子窗体事件——void Handle(SetChildWindowEvent childWindowEvent)
        /// <summary>
        /// 显示子窗体事件
        /// </summary>
        public override void Handle(ShowChildWindowEvent eventData)
        {
            var childWindowViewModel = eventData.ChildWindowViewModel as ChildWindowViewModelBase;
            if (childWindowViewModel == null) return;

            Wpf.Controls.Controls.ChildWindow.Show(childWindowViewModel, eventData.ShowMask);
        }
        #endregion

        #region # 显示消息事件方法——void Handle(MessageEvent eventData)
        /// <summary>
        /// 显示消息事件方法
        /// </summary>
        public override void Handle(MessageEvent eventData)
        {
            switch (eventData.MessageType)
            {
                //case MessageType.Info:
                //    Growl.Info(eventData.Message);
                //    break;
                //case MessageType.Success:
                //    Growl.Success(eventData.Message);
                //    break;
                //case MessageType.Warning:
                //    Growl.Warning(eventData.Message);
                //    break;
                //case MessageType.Error:
                //    Growl.Error(eventData.Message);
                //    break;
                //case MessageType.Ask:
                //    Growl.Ask(eventData.Message, (f1) =>
                //    {
                //        return true;
                //    });
                //    break;
                //case MessageType.Fatal:
                //    Growl.Fatal(eventData.Message);
                //    break;
            }
        }
        #endregion
        #endregion
    }
}
